/*
 * @fileOverview The utilities of Accedo Memeory Game
 * @author Bo Liang
 */

if (typeof accedo === "undefined") {
	accedo = {};
}
if (typeof accedo.memoryGame === "undefined") {
	accedo.memoryGame = {};
}

accedo.memoryGame.util = {
		
	/**
	 * create and return a two-dimensional array that contains two groups equal numbers.
	 * like a 4*4 array, it has 16 cells and contains two groups equal numbers from 1 to 8.
	 * @param {String} rowNum the row number of the card matrix
	 * @param {String} colNum the column number of the card matrix
	 * @returns {Array} a two-dimensional array
	 */			
	randomArr : function(rowNum, colNum) {
		var arr = new Array();
		var randomUpper = rowNum * colNum;
		var randomPosition = null;

		// Create array stored the numbers using for pairing
		var numPairArr = new Array();
		for ( var y = 0; y < rowNum * colNum; y++) {
			numPairArr[y] = y + 1;
		}

		// Create randomArr two-dimensional array
		for ( var rowX = 0; rowX < rowNum; rowX++) {
			arr[rowX] = new Array();
			for ( var colX = 0; colX < colNum; colX++) {
				randomPosition = parseInt(Math.random() * (randomUpper - 1 + 1)
						+ 1) - 1;
				arr[rowX][colX] = numPairArr[randomPosition];
				if (arr[rowX][colX] > rowNum * colNum / 2) {
					arr[rowX][colX] = arr[rowX][colX] - rowNum * colNum / 2;
				}
				numPairArr.remove(randomPosition);
				randomUpper--;
			}
		}
		return arr;
	},

	/**
	 * parse the URL and put the arguments value into the args array, then return the array
	 * @returns {Object} args the array contains the arguments value
	 */
	GetUrlParms : function() {
		var args = new Object();
		var query = location.search.substring(1);
		var pairs = query.split("&");
		for ( var i = 0; i < pairs.length; i++) {
			var pos = pairs[i].indexOf('=');
			if (pos == -1)
				continue;
			var argname = pairs[i].substring(0, pos);
			var value = pairs[i].substring(pos + 1);
			args[argname] = unescape(value);
		}
		return args;
	},
	
	time : {
		countTimeNum : 0,
		timeOutID : null,
		timeCount : function() {
			document.getElementById('timeTextID').value = this.countTimeNum;
			this.countTimeNum = this.countTimeNum + 1;
			this.timeOutID = setTimeout("accedo.memoryGame.util.time.timeCount()", 1000);
		}	
	}
};

/**
 * add remove functionality to array
 * @param {Object} obj the element wanted to be removed from Array
 */
Array.prototype.remove = function(obj) {
	for ( var i = 0; i < this.length; i++) {
		var temp = this[i];
		if (!isNaN(obj)) {
			temp = i;
		}
		if (temp == obj) {
			for ( var j = i; j < this.length; j++) {
				this[j] = this[j + 1];
			}
			this.length = this.length - 1;
		}
	}
};